﻿import { ref } from 'vue'
import { http } from "@/axios/index.js";
import { message } from "ant-design-vue";
export const columns = ref(
    #listColumnsList
)

export const rowList = ref(#editFormOptions)
export const searchList = ref(#searchFormOptions)

export const detailRow = ref(#DetailformOptions)
export const detail = ref({
    cnName: "#detailCnName",
    table: "#detailTable",
    columns: #detailColumns,
    sortName: "#detailSortName",
    key: "#detailKey"
})
export const sorter = ref(#sorter)
export const getDictonLibry = async () => {
    let keys = []
    columns.value.map((x) => {
        if (x.bind && x.bind.key && (!x.bind.data || x.bind.data.length == 0)) {
            if (!x.bind.data) x.bind.data = [];
            keys.push(x.bind.key);
            x.bind.valueType = x.e_type;
        }
    })
    const data = rowList.value.flat(Infinity)
    let editKeys = []
    data.map(v => {
        if (v.key) {
            editKeys.push(v.key)
        }
    })
    if (editKeys.length != 0) {
        const { Success, Data, Msg } = await http.post('/Utility/GetVueDictionary/api/GetVueDictionary', editKeys)
        if (Success) {
            Data.map(v => {
                data.map(x => {
                    if (x && x.key == v.dicNo) {
                        const datas = v.data.map((item) => {
                            return { label: item.value, value: item.key }
                        });
                        x.data = datas
                    }
                })
            })
        } else {
            message.error(Msg)
        }
    }
    if (keys.length != 0) {
        try {
            const { Success, Data, Msg } = await http.post('/Utility/GetVueDictionary/api/GetVueDictionary', keys)
            if (Success) {
                Data.map(v => {
                    columns.value.map(x => {
                        if (x.bind && x.bind.key == v.dicNo) {
                            const datas = v.data.map((item) => {
                                return { label: item.value, value: item.key }
                            });
                            x.bind.data = datas
                        }
                    })

                })
            } else {
                message.error(Msg)
            }
        } catch (err) {
            message.error("服务器异常")
        }
    }
    let searchkeys = []
    if (searchList.value.length == 0) return
    searchList.value.map((x) => {
        if (x.bind && x.bind.key && (!x.bind.data || x.bind.data.length == 0)) {
            if (!x.bind.data) x.bind.data = [];
            searchkeys.push(x.bind.key);
            x.bind.valueType = x.e_type;
        }
    })

    if (searchkeys.length != 0) {
        try {
            const { Success, Data, Msg } = await http.post('/Utility/GetVueDictionary/api/GetVueDictionary', searchkeys)
            if (Success) {
                Data.map(v => {
                    searchList.value.map(x => {
                        if (x.bind && x.bind.key == v.dicNo) {
                            const datas = v.data.map((item) => {
                                return { label: item.value, value: item.key }
                            });
                            x.bind.data = datas
                        }
                    })

                })
            } else {
                message.error(Msg)
            }
        } catch (err) {
            message.error("服务器异常")
        }
    }

    // 子表table
    let sonkeys = []
    detail.value.columns.map((x) => {
        if (x.bind && x.bind.key && (!x.bind.data || x.bind.data.length == 0)) {
            if (!x.bind.data) x.bind.data = [];
            sonkeys.push(x.bind.key);
            x.bind.valueType = x.e_type;
        }
    })
    if (sonkeys.length != 0) {
        try {
            const { Success, Data, Msg } = await http.post('/Utility/GetVueDictionary/api/GetVueDictionary', sonkeys)
            if (Success) {
                Data.map(v => {
                    detail.value.columns.map(x => {
                        if (x.bind && x.bind.key == v.dicNo) {
                            const datas = v.data.map((item) => {
                                return { label: item.value, value: item.key }
                            });
                            x.bind.data = datas
                        }
                    })
                })
            } else {
                message.error(Msg)
            }
        } catch (err) {
            message.error("服务器异常")
        }
    }
    // 子表表单
    const detailData = detailRow.value.flat(Infinity)
    let detailKeys = []
    detailData.map(v => {
        if (v.key) {
            detailKeys.push(v.key)
        }
    })
    if (detailKeys.length != 0) {
        const { Success, Data, Msg } = await http.post('/Utility/GetVueDictionary/api/GetVueDictionary', detailKeys)
        if (Success) {
            Data.map(v => {
                detailData.map(x => {
                    if (x && x.key == v.dicNo) {
                        const datas = v.data.map((item) => {
                            return { label: item.value, value: item.key }
                        });
                        x.data = datas
                    }
                })
            })
        } else {
            message.error(Msg)
        }
    }
}



